﻿#region Usings

using System;
using System.Linq;
using System.Web.Mvc;
using BookID.Data;

#endregion

namespace BookID.Areas.Admin.Controllers
{
    public class AdminUserController : Controller
    {
        private readonly Repository _repository = new Repository();

        //
        // GET: /Admin/User/

        public ActionResult Index()
        {
            /*var userViewModel = new List<UserModel>();
            foreach (var userModel in _repository.BaseSubjects.OfType<User>())
            {
                userViewModel.Add(new UserModel() { User = userModel });
            }*/

            return View(_repository.BaseSubjects.OfType<User>());
        }

        //
        // GET: /Admin/User/Details/5

        public ActionResult Details(Guid id)
        {
            var userModel = _repository.BaseSubjects.OfType<User>().SingleOrDefault(user => user.Id == id);


            //// ovo predajemo na view
            //var userViewModel = new UserModel();
            //userViewModel.User = userModel;

            // todo: ubaci userViewModel u View funkciju

            return View(userModel);
        }

        //
        // GET: /Admin/User/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/User/Create

        [HttpPost]
        public ActionResult Create(User user)
        {
            try
            {
                user.Id = Guid.NewGuid();
                _repository.BaseSubjects.AddObject(user);
                _repository.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(user);
            }
        }

        //
        // GET: /Admin/User/Edit/5

        public ActionResult Edit(Guid id)
        {
            var userModel = _repository.BaseSubjects.OfType<User>().SingleOrDefault(user => user.Id == id);
            return View(userModel);
        }

        //
        // POST: /Admin/User/Edit/5

        [HttpPost]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            try
            {
                var userModel = _repository.BaseSubjects.OfType<User>().SingleOrDefault(user => user.Id == id);
                UpdateModel(userModel, collection.ToValueProvider());
                _repository.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/User/Delete/5

        public ActionResult Delete(Guid id)
        {
            var userModel = _repository.BaseSubjects.OfType<User>().SingleOrDefault(user => user.Id == id);
            return View(userModel);
        }

        //
        // POST: /Admin/User/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                var userModel = _repository.BaseSubjects.OfType<User>().SingleOrDefault(user => user.Id == id);
                _repository.DeleteObject(userModel);
                _repository.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}